package com.springcloud.sc.ucpv5.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTool {

	private static Date standardDate = new Date(0);// PublicUtil.stringToDate("1970-01-01","yyyy-mm-dd");

	/**
	 * 以1970-1-1的时间为基准,转换int值为时间
	 * 
	 * @date 2006-7-11
	 * @param time
	 * @return
	 * @see
	 */
	public static Date intToDate(int time) {
		if (time < 0) {
			return new Date(standardDate.getTime());
		} else {
			long tmp = time;
			tmp = tmp * 1000;
			tmp += standardDate.getTime();

			return new Date(tmp);
		}
	}

	public static Date longToDate(long time) {
		if (time < 0) {
			return new Date(standardDate.getTime());
		} else {
			time = time * 1000;
			time += standardDate.getTime();

			return new Date(time);
		}
	}

	/**
	 * 以1970-1-1的时间为基准,转换时间值为int
	 * 
	 * @date 2006-7-11
	 * @param date
	 * @return
	 * @see
	 */
	public static int dateToInt(Date date) {
		long minTime = standardDate.getTime();
		long curTime = date.getTime();
		long ret = (curTime - minTime) / 1000;
		if (ret < 0) {
			return 0;
		} else if (ret > Integer.MAX_VALUE) {
			return Integer.MAX_VALUE;
		} else {
			return (int) ret;
		}
	}

	/**
	 * 从日期对象转为日期字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate_yyyMMdd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate_yyyyMMddHHmmss(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";

	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = null;
		try {
			date = sdf.parse(dateString.trim());
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString_yyyyMMdd(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	public static String getCurData() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurData_yyyyMMdd() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	/**
	 * time1小于等于time2时返回true
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean compareTimeFromHHmmss(String time1, String time2) {
		String[] str1 = time1.split(":");
		String[] str2 = time2.split(":");

		if (Integer.parseInt(str1[0]) > Integer.parseInt(str2[0])) {
			return false;
		} else if (Integer.parseInt(str1[0]) == Integer.parseInt(str2[0])) {
			if (Integer.parseInt(str1[1]) > Integer.parseInt(str2[1])) {
				return false;
			} else if (Integer.parseInt(str1[1]) == Integer.parseInt(str2[1])) {
				if (Integer.parseInt(str1[2]) > Integer.parseInt(str2[2])) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 判断是否是周末
	 * 
	 * @return
	 */
	public static boolean isWeekend() {
		Calendar cal = Calendar.getInstance();
		int week = cal.get(Calendar.DAY_OF_WEEK);
		if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) {
			return true;
		}
		return false;
	}

	public static long getCurTime_Long() {
		return (new Date()).getTime();
	}

	/**
	 * 取得当前日期时间的字符串类型
	 * 
	 * @return
	 */
	public static String getCurDataTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String curDate = null;
		try {
			curDate = dateFormat.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

}
